/*
 * MatchingResource.cpp
 *
 *  Created on: 2013. 9. 10.
 *      Author: mega@gamevil.com
 */

#include "../User.h"
#include "MatchingResource.h"
#include "../NUtil/Utils.h"
#include "../engine.h"

namespace ProjectZ { namespace NResource {

MatchingResource::MatchingResource()
{
	pMatchingAD	= new MatchingData;
	pMatchingIOS		= new MatchingData;
}

MatchingResource::~MatchingResource()
{
	delete pMatchingAD;
	delete pMatchingIOS;
}

bool MatchingResource::LoadResource()
{
	if (false == pMatchingAD->LoadResource(MARKET_GOOGLE, SC_KAKAO_GOOGLE))
		return false;

	if (false == pMatchingIOS->LoadResource(MARKET_IOS, SC_KAKAO_IOS))
		return false;

	return true;
}

MatchingData::Flyweight * MatchingResource::GetFly(int company, int saleCode, int type)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(company, saleCode);

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pMatchingAD->GetFly(type);

	case NUtil::MarketType::IOS_KAKAO:
		return pMatchingIOS->GetFly(type);

	default:
		return NULL;
	}

	return NULL;
}

MatchingData::~MatchingData()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
}

void
MatchingData::Clear()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
	clsFlyTree.clear();
}

bool MatchingData::LoadResource(int company, int saleCode)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT NATIONAL_CODE, type, min_level, max_level, rate FROM resource_matching WHERE company=%d AND sale_cd=%d", company, saleCode);

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	Clear();

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		MatchingData::Flyweight * pFly = new MatchingData::Flyweight;

		clsAuto->GetRecord() >> NATIONAL_CODE >> pFly->_TYPE >> pFly->_MIN_LEVEL >> pFly->_MAX_LEVEL >> pFly->_RATE;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsFlyTree[pFly->_TYPE] = pFly;
	}

	return true;
}


MatchingData::Flyweight * MatchingData::GetFly(int type)
{
	auto iter = clsFlyTree.find(type);

	if (iter == clsFlyTree.end())
		return NULL;

	return iter->second;
}

bool MatchingResource::CheckLevel(User * pUser, User * pOppUser, int gradeType)
{
	MatchingData::Flyweight * pFly = this->GetFly(pUser->GetUserInfo().company, pUser->GetUserInfo().sale_code, gradeType);

	if (pFly == NULL)
		return false;

	int levelGap = pOppUser->GetCharacterInfo().level - pUser->GetCharacterInfo().level;

	if (pFly->_MIN_LEVEL <= levelGap && levelGap <= pFly->_MAX_LEVEL)
		return true;

	return false;

//	int myLevel = pUser->GetCharacterInfo().level;
//	int oppLevel = pOppUser->GetCharacterInfo().level;
//
//	int minLevel = oppLevel + pFly->_MIN_LEVEL;
//	int maxLevel = oppLevel + pFly->_MAX_LEVEL;
//
//	if (myLevel < minLevel || myLevel > maxLevel)
//		return false;
//
//	return true;
}

} /* NResource */ } /* ProjectZ */

